package cn.stronglink.asset.module.assetmanage.instock.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.Instock;
import cn.stronglink.asset.model.InstockDetail;
import cn.stronglink.asset.module.assetmanage.instock.service.InstockService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;

@Controller
@RequestMapping(value = "instock")
public class InstockController extends AbstractController {

	@Autowired
	private InstockService instockService;

	// 查询所有入库单
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryPutOnList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("operatorBy", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<Instock> page = (Page<Instock>) super.getPage(params);
		Page<Instock> data = instockService.queryList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	// 根据上架单Id查询明细
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> qryById(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {	
		Instock data =instockService.queryInstockById(id);
		List<InstockDetail> detail = instockService.qryDetailInfoByInstockId(id);
		modelMap.put("dataVo", detail);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 新增入库单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param PutOn
	 * @return
	 */
	@OperateLog(module = "入库单管理",desc="添加入库单", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Instock instock) {
		instockService.create(instock);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 编辑入库单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "入库单管理",desc="编辑入库单", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Instock instock) {
		instockService.edit(instock);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 修改上架U位信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "入库单管理",desc="确认入库", type = OpType.UPDATE)
	@RequestMapping(value = "confirmInstock")
	public ResponseEntity<ModelMap> confirmInstock(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Instock instock) {
		try {
			instockService.confirmInstock(instock);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除入库单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "入库单管理",desc="删除入库单", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		instockService.remove(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 修改入库单状态
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param instock
	 * @return
	 */
	@OperateLog(module = "入库单管理",desc="修改入库单状态", type = OpType.UPDATE)
	@RequestMapping(value = "updateStatus")
	public ResponseEntity<ModelMap> updateStatus(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Instock instock) {
		instockService.updateStatus(instock.getId(),instock.getStatus());
		return setSuccessModelMap(modelMap, null);
	}
	
}
